package com.zj.order.service.impl;

import com.zj.order.client.UserClient;
import com.zj.order.mapper.OrderMapper;
import com.zj.order.model.dto.OrderByUserDto;
import com.zj.order.model.dto.UserDto;
import com.zj.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @program cloud-demo
 * @description:
 * @author: zhangbo
 * @create: 2022/02/03 01:03
 */
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public OrderByUserDto queryOrderById(Long id) {

        return orderMapper.selectOrderById(id)
                .filter(o -> {
                    return !StringUtils.isEmpty(o.getUserId());
                })
                .map(order -> {
                    UserDto user = userClient.findById(order.getUserId());
                    return new OrderByUserDto(order, user);
                }).orElse(null);
    }
}
